//
//  WCNLoginVC.m
//  WCN
//
//  Created by 杨鑫 on 15/5/29.
//  Copyright (c) 2015年 ShanghaiCize Trade And Business Co., Ltd. All rights reserved.
//


#import "BTLoginVC.h"
//VC
#import "HELForgotPwdVC.h"
#import "NSString+Utilities.h"
#import "AppDelegate.h"
#import "WCBaiduPushBindHelp.h"


@interface BTLoginVC () <UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;
@property (weak, nonatomic) IBOutlet UITextField *pwdTextField;

@end

@implementation BTLoginVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setTitle:@"登录"];
    self.pwdTextField.delegate = self;
    self.phoneTextField.text = [HELAppManager userInfo].busnEmailAddr;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

#pragma mark - Nib Targets
//click backgroundView
- (IBAction)bgTouchDown:(id)sender
{
    
//    [self.phoneTextField hideList];
    [self.view endEditing:YES];
}

//忘记密码
- (IBAction)showPwdVC:(id)sender {
    HELForgotPwdVC *pwdVC = [HELForgotPwdVC new];
    pwdVC.title = @"找回密码";
    [self.navigationController pushViewController:pwdVC animated:YES];
}

// 点击登录
- (IBAction)doLogin:(id)sender {

  
    
    [self.view endEditing:YES];
    NSString *userAcount = self.phoneTextField.text, *password = self.pwdTextField.text;
    if (!userAcount) {
        alertMessage(@"邮箱不能为空～");
      
        return;
    }
    
    if (![userAcount isEmail]) {
        alertMessage(@"请输入正确的邮箱账号～");
        return;
    }

    if ([password isEmpty]) {
        alertMessage(@"密码不能为空～");
        return;
    }
    
    
    
    cwqHud;
    weakSelf_define;
    NSDictionary *param = @{@"email":userAcount, @"password":password};
    [[BTNetworkManager sharedMg] NoSession_POST:BTLoginUrl parameters:param success:^(id responseObject) {
        if (responseObject_success) {
            
            NSMutableDictionary *muDict = [responseObject[@"data"][@"user"] mutableCopy];
            NSMutableDictionary *dict =[NSMutableDictionary dictionaryWithDictionary:responseObject[@"data"]];
            muDict[@"password"] = password;
            dict[@"user"] = muDict;
            [HELAppManager saveUserInfoAndCookie:dict];
            //wsjtest
            [WCBaiduPushBindHelp bindToService];
            [[NSNotificationCenter defaultCenter] postNotificationName:LoginSuccessNotification object:nil];
            [weakSelf dismissViewControllerAnimated:YES completion:nil];
            cwqAlertMessage(@"登录成功");
        }
        
    } failure:^(NSString *err) {
    }];
}

- (void)dealloc {

    [[NSNotificationCenter defaultCenter] removeObserver:self];

}

#pragma mark - Notification

- (void)keyboardWillHide:(NSNotification *)note
{
    [UIView animateWithDuration:0.25
                          delay:0.0
                        options:7<<16
                     animations:^{
                         self.view.transform = CGAffineTransformIdentity;
                     } completion:NULL];
}

#pragma mark - UITextFieldDelegate

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    
    CGRect rect = [textField convertRect:textField.bounds toView:self.view];
    CGFloat y = self.view.frame.size.height - rect.origin.y - rect.size.height - kKeyboardHeight;
    
#warning 为了配合键盘弹出的动画，使用7<<16.
    [UIView animateWithDuration:0.25
                          delay:0.0
                        options:7<<16
                     animations:^{
                         self.view.transform = CGAffineTransformMakeTranslation(0, y>=0?0:y-10);
                     } completion:NULL];
    
    return YES;
}

- (BOOL)textFieldShouldClear:(UITextField *)textField
{
    if (textField.tag == 0) {
        self.pwdTextField.text = @"";
    }
    
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField.tag == 1) {
        [textField resignFirstResponder];
        [self doLogin:nil];
        return YES;
    }
    
    return NO;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    if (textField.tag ==0) {
//        [self showSuggestList];
    }
}
@end
